﻿<%@ WebHandler Language="C#" Class="request" %>

using System;
using System.Web;

public class request : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        string cmd = context.Request.Form["cmd"];
        if (cmd == "login")
        {
            string u = context.Request.Form["u"];
            string p = context.Request.Form["p"];
            string sql = "select * from [admin] where username='" + u + "' and password='" + p + "'";
            if (SQLHelper.GetReader(sql).Read())
            {
                context.Response.Write("ok");
            }
            else
            {
                context.Response.Write("no");
            }
            return;
        }
        if (cmd == "bumen")
        {
            string sql = "select * from bumen";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "zhiwei")
        {
            string sql = "select * from zhiwei";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "luru")
        {
            string bumen = context.Request.Form["bumen"];
            string zhiwei = context.Request.Form["zhiwei"];
            string nicheng = context.Request.Form["nicheng"];
            string age = context.Request.Form["age"];
            string phone = context.Request.Form["phone"];
            string qq = context.Request.Form["qq"];
            string timer = DateTime.Now.ToString();
            if (SQLHelper.ExcuteSQL(string.Format("insert into people(bumen,zhiwei,nicheng,age,phone,qq,timer) values ({0},{1},'{2}','{3}','{4}','{5}','{6}')",bumen, zhiwei, nicheng, age, phone, qq, timer)) > 0)
            {
                context.Response.Write("ok");
            }
            else
            {
                context.Response.Write("no");
            }
            return;
        }
        if (cmd == "people")
        {
            string tb = ConvertJson.ToJson(SQLHelper.GetTable("select rid,a.title as bumen,b.title as zhiwei,nicheng,age,phone,qq,timer from people as c,bumen as a,zhiwei as b where c.bumen=a.bid and c.zhiwei=b.zid"));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "sousuo")//搜索
        {
            string sql = "";
            string key = context.Request.Form["key"];
            if (IsNumberic(key) == true)
            {
                sql = " and (c.rid=" + key+" or c.qq='"+key+"')";
            }
            else
            {
                sql = " and c.nicheng='" + key+"'";
            }
            string zsql = "select rid,a.title as bumen,b.title as zhiwei,nicheng,age,phone,qq,timer from people as c,bumen as a,zhiwei as b where c.bumen=a.bid and c.zhiwei=b.zid" + sql;
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(zsql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "update")//编辑
        {
            string rid = context.Request.Form["rid"];
            Int32 col = Convert.ToInt32( context.Request.Form["col"]);
            string update = context.Request.Form["update"];
            string sql = "";
            switch (col)
            {
                case 1: sql = "update people set bumen=" + getbid(update) + " where rid=" + rid; break;
                case 2: sql = "update people set zhiwei=" + getzid(update) + " where rid=" + rid; break;
                case 3: sql = "update people set nicheng='" + update + "' where rid=" + rid; break;
                case 4: sql = "update people set age='" + update + "' where rid=" + rid; break;
                case 5: sql = "update people set phone='" + update + "' where rid=" + rid; break;
                case 6: sql = "update people set qq='" + update + "' where rid=" + rid; break;
                case 7: sql = "update people set timer='" + update + "' where rid=" + rid; break;
            }
            if (SQLHelper.ExcuteSQL(sql) > 0)
            {
                context.Response.Write("ok");
            }
            return;
        }
        
    }
    public int getbid(string bumen)
    {
        int bid=Convert.ToInt32( SQLHelper.GetTable("select bid from bumen where title='"+bumen+"'").Rows[0][0].ToString());
        return bid;
    }
    public int getzid(string zhiwei)
    {
        int zid = Convert.ToInt32(SQLHelper.GetTable("select zid from zhiwei where title='" + zhiwei + "'").Rows[0][0].ToString());
        return zid;
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    private bool IsNumberic(string oText)
    {
        try
        {
            int var1 = Convert.ToInt32(oText);
            return true;
        }
        catch
        {
            return false;
        }
    }

}